package com.mobile.challenge.service.impl;

import com.mobile.challenge.dao.PrizeMapper;
import com.mobile.challenge.dao.SignUpRecordMapper;
import com.mobile.challenge.dao.UserMapper;
import com.mobile.challenge.model.SignUpRecord;
import com.mobile.challenge.model.User;
import com.mobile.challenge.service.SignUpRecordService;
import com.mobile.challenge.core.AbstractService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by CodeGenerator on 2018/06/08.
 */
@Service
@Transactional
public class SignUpRecordServiceImpl extends AbstractService<SignUpRecord> implements SignUpRecordService {
    @Resource
    private SignUpRecordMapper tSignUpRecordMapper;
    @Resource
    private PrizeMapper tPrizeMapper;
    @Resource
    private UserMapper tUserMapper;

    /**
     * 是否已报名
     * @param phoneNo
     * @return
     */
    @Override
    public Map findStatusByPhoneNo(String phoneNo) {

        //获取报名有效期
        int signUpIndate = tPrizeMapper.selectByPrimaryKey(1).getSignUpIndate();
        //查询有效期內此用户报名情况
        List<User> userList = tUserMapper.queryByPhoneNoAndSingInDate(phoneNo);

        Map data = new HashMap();
        /*
            已访问
         */
        if(userList.size() != 0){
            /*
                报名失效
             */
            if( 0 == userList.get(0).getStatus()){
                data.put("code",0);
                data.put("msg","rookie");
            }
            /*
                报名有效
             */
            else{
                data.put("code",1);
                data.put("msg","old hand");
            }

        }
        /*
            未访问
         */
        else{
            data.put("code",0);
            data.put("msg","rookie");
        }
        return data;
    }
    /**
     * 报名
     * @param signUpRecord
     * @return
     */
    @Override
    public Map signUp(SignUpRecord signUpRecord) {

        Map data = new HashMap();
        try {

            User user = new User();
            user.setPhoneNo(signUpRecord.getPhoneNo());
            //记录报名动作
            tSignUpRecordMapper.insertSelective(signUpRecord);
            //修改用户报名状态
//            tUserMapper.updateStatusByPhoneNoAndSingInDate(1,user.getPhoneNo());

            data.put("code",true);
            data.put("msg","报名成功");
        } catch (Exception e) {
            data.put("code",false);
            data.put("msg","报名失败");
            e.printStackTrace();
            return data;
        }
        return data;
    }
}
